/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.latestbit.smartobjects.ISmartBus;


public class EndpointLocation implements IEndpointLocation {
	
	private String coordinatorName = null; // new StdFailOverCoordinator();
	private Collection<URI> locations = new LinkedList<URI>();
	
	public EndpointLocation(ISmartBus bus) {
		this.coordinatorName = bus.getCoordinatorFactory().getDefaultCoordinatorName();
	}
	
	public EndpointLocation(String coordinatorName) {
		this.coordinatorName = coordinatorName;
	}
	
	public EndpointLocation(ISmartBus bus, URI location) {
		this.coordinatorName = bus.getCoordinatorFactory().getDefaultCoordinatorName();
		locations.add(location);
	}
	
	public EndpointLocation(ISmartBus bus, URI ... uriLocations) throws URISyntaxException {
		this.coordinatorName = bus.getCoordinatorFactory().getDefaultCoordinatorName();
		for(URI location: uriLocations) {
			locations.add(location);
		}
	}	
	
	
	public EndpointLocation(ISmartBus bus, String ... strLocations) throws URISyntaxException {
		this.coordinatorName = bus.getCoordinatorFactory().getDefaultCoordinatorName();
		for(String location: strLocations) {
			locations.add(new URI(location));
		}
	}
	
	public EndpointLocation(ISmartBus bus, Collection<String> locations) throws URISyntaxException {
		this.coordinatorName = bus.getCoordinatorFactory().getDefaultCoordinatorName();
		for(String item : locations) {
			this.locations.add(new URI(item));
		}
	}
	
	public EndpointLocation(ISmartBus bus, String reference) throws URISyntaxException {
		
		String ref = new String(reference);
		ref = ref.trim();

		int findCrd = ref.indexOf("{");

		String coordName = null;
		if(findCrd!=-1) {
			coordName = ref.substring (0, findCrd);
			coordName = coordName.trim();
			ref = ref.substring(findCrd + 1, ref.length() - 2);
		}
		
		StringTokenizer tokenizer = new StringTokenizer(ref, ";");
		
		while(tokenizer.hasMoreTokens()) {
			String locationUrl = tokenizer.nextToken().trim();			
			this.locations.add(new URI(locationUrl));
		}
		
		if(coordName!=null) {
			this.coordinatorName = coordName;
		}
		else
			this.coordinatorName = bus.getCoordinatorFactory().getDefaultCoordinatorName();
	}

	
	@Override
	public String getCoordinator() {
		return this.coordinatorName;
	}
	
	@Override
	public void setCoordinator(String coordinatorName) {
		this.coordinatorName = coordinatorName;
	}

	@Override
	public Collection<URI> getLocations() {
		return locations;
	}

	@Override
	public void setLocations(Collection<URI> locations) {
		this.locations = locations;
	}

	@Override
	public String toString() {
		return toStringReference();
	}
	
	@Override
	public String toStringReference() {
		String result="";
		SortedSet<String> sortedLocations = new TreeSet<String>();
		for(URI uriLocation : getLocations()) {
			sortedLocations.add(uriLocation.getScheme()+"://"+uriLocation.getHost()+":"+uriLocation.getPort());
		}
		
		Iterator<String> it = sortedLocations.iterator();
		while(it.hasNext()) {			
			result=result+it.next();
			if(it.hasNext()) {
				result=result+";";
			}
		}
		
		if(this.coordinatorName!=null) {
			result= this.coordinatorName + "{" + result + "}";
		}
		
		return result;
	}

}
